package com.hubei.controller;

import com.google.gson.Gson;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class TreeController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        httpServletRequest.setCharacterEncoding("UTF-8");
        String name= httpServletRequest.getParameter("name");
        System.out.println(name);
        //返回json格式数据
        Map<String,Object> map=new HashMap<>();
        map.put("name","p1");
        map.put("age",30);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
//        String result=null;
//        JsonFactory build= new JsonFactoryBuilder().build();
//        JsonParser parser=null;
//        parser.
//        httpServletResponse.getWriter().print(result);
//

            Gson gson=new Gson();
            String result=gson.toJson(map);
//        try{
//            Thread.sleep(5000);
//        }catch (Exception e){
//            e.printStackTrace();
//
//        }
            PrintWriter writer=httpServletResponse.getWriter();
            writer.print(result);
            writer.close();

    }
}
